﻿@model WayCloud.ViewModel.Menu.MenuCondition
@{
    ViewBag.Title = "数据中心管理 ";
}
<script type="text/javascript">
    var controllerName = 'menu';

    function createEditLink(cellvalue, options, rowData) {
        return "<a href='##' name='editLink' rowIndex='" + options.rowId + "' style='color:#f60'>修改</a>";
    }

    function createDeleteLink(cellvalue, options, rowData) {
        return "<a href='##' name='deleteLink' rowIndex='" + options.rowId + "' style='color:#f60'>删除</a>";
    }

    function formatterOpenNewWindow(cellvalue, options, rowData) {
        if (cellvalue.toString().toLocaleLowerCase() === "true") {
            return "是";
        }
        return "否";
    }

    function createOrderLink(cellvalue, options, rowData) {
        return "<a href='##' name='upLink' rowIndex='" + options.rowId + "' style='color:#f60'>上移</a>"
            + "&nbsp;&nbsp;&nbsp;"
            + "<a href='##' name='downLink' rowIndex='" + options.rowId + "' style='color:#f60'>下移</a>";
    }

    function serializeCondition() {
        return $("#divSearch").children().serialize();
    }

    function getRowData(rowIndex) {
        return $("#list").getRowData(rowIndex);
    }

    function createModelDialog(title, width, height) {
        $("#editDiv").dialog({
            modal: true,
            width: width,
            title: title,
            draggable: false,
            resizable: false,
            open: function (event) {
                $("#editDiv").data("dialog", event.target);
            },
            beforeClose: function () {
                $("#list").trigger("reloadGrid");
                $("#editDiv").dialog("destroy");
            }
        });
    }

    function reorder(id1, id2) {
        $.get("/menu/reorder/" + id1 + "|" + id2, function (result) {
            if (result == "ok") {
                $("#list").jqGrid().trigger("reloadGrid");
            }
            else {
                alert(result);
            }
        });
    }

    function gridComplete() {
        //删除链接
        $("a[name='deleteLink']").click(function () {
            var rowData = getRowData($(this).attr("rowIndex"));
            if (!confirm("确定要删除【" + rowData.Name + "】")) {
                return;
            }
            $.get("/" + controllerName + "/delete/" + rowData.Id, function (result) {
                if (result == "ok") {
                    alert("删除成功！");
                    $("#list").jqGrid().trigger("reloadGrid");
                }
                else {
                    alert(result);
                }
            });
        });

        //修改链接
        $("a[name='editLink']").click(function () {
            var rowData = getRowData($(this).attr("rowIndex"));
            $("#editDiv").load('/' + controllerName + '/find/' + rowData.Id, function () {
                createModelDialog("修改链接：" + rowData.Name, 800);
            });
        });

        $("a[name='upLink']").click(function () {
            var currentRowIndex = $(this).attr("rowIndex");
            var upRowIndex = currentRowIndex - 1;
            if (upRowIndex < 0) {
                return;
            }
            var currentId = getRowData(currentRowIndex);
            var upId = getRowData(upRowIndex);
            reorder(currentId, upId);
        });

        $("a[name='downLink']").click(function () {
            var currentRowIndex = $(this).attr("rowIndex");
            var downRowIndex = currentRowIndex + 1;
            var records = $('#list').getGridParam('records') - 1;
            alert(records);
            if (downRowIndex > records) {
                return;
            }
            var currentId = getRowData(currentRowIndex);
            var upId = getRowData(downRowIndex);
            reorder(currentId, upId);
        });
    }

    $().ready(function () {
        $.ajaxSetup({ cache: false });
        $("#list").jqGrid({
            url: '/' + controllerName + '/list/',
            datatype: "json",
            colNames: ['编号', '名称', '链接地址', '在新窗口打开', '排序', '修改', '删除'],
            colModel: [
                { name: 'Id', index: 'Id', width: 150, align: 'center', hidden: true, sortable: false },
                { name: 'Name', index: 'Name', width: 150, align: 'left', sortable: false },
                { name: 'Href', index: 'Href', width: 400, align: 'left', sortable: false },
                { name: 'OpenNewWindow', index: 'OpenNewWindow', width: 100, align: 'center', sortable: false, formatter: formatterOpenNewWindow },
                { name: 'Order', index: 'Order', width: 100, align: 'center', sortable: false, formatter: createOrderLink, hidden: true },
                { name: 'Modify', index: 'Id', width: 60, align: 'center', sortable: false, formatter: createEditLink },
                { name: 'Delete', index: 'Id', width: 60, align: 'center', sortable: false, formatter: createDeleteLink }
            ],
            autowidth: true,
            hidegrid: false,
            height: "auto",
            rowNum: 100,
            pager: '#pager',
            emptyrecords: "没有满足条件的记录",
            viewrecords: true,
            caption: "@ViewBag.Title",
            multiselect: false,
            jsonReader: {
                repeatitems: false
            },
            gridComplete: gridComplete
        }).navGrid('#pager', { edit: false, add: false, del: false, view: false, search: false });

        $("#btnSearch").button().click(function () {
            var url = '/' + controllerName + '/list/' + serializeCondition();
            $("#list").jqGrid('setGridParam', { url: url }).trigger("reloadGrid");
        });

        $("#btnAdd").button().click(function () {
            var url = '/' + controllerName + '/find/0';
            $.get(url, function (result) {
                $("#editDiv").html(result);
                createModelDialog("新建功能链接", 600);
            });
        });

    });
</script>
<div>
    <div style="margin: 5px; text-align: left;" id="divSearch">
        @Html.LabelFor(p => p.Name)：@Html.EditorFor(p => p.Name)@Html.ValidationMessageFor(p => p.Name)
        <input type="button" id="btnSearch" value="查  询" />
        <input type="button" id="btnAdd" value="新 增" />
    </div>
    <table id="list" width="100%">
    </table>
    <div id="pager">
    </div>
</div>
<div id="editDiv">
</div>
